<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_select">
        <div class="input">
          <el-select
            v-model="dataForm.organization_id"
            :placeholder="i18n('common.please_select') + i18n('organization.title')"
            clearable>
            <el-option :label="i18n('common.all')" value=""></el-option>
            <el-option v-for="(v, k) in organizationList" :label="v.title" :key="k" :value="v.id">
            </el-option>
          </el-select>
        </div>
        <!-- <div class="input">
          <el-select
            v-model="dataForm.client_id"
            :placeholder="i18n('common.please_select') + i18n('client.name')"
            clearable>
            <el-option :label="i18n('common.all')" value=""></el-option>
            <el-option v-for="(v, k) in clientList" :label="v.name" :key="k" :value="v.id">
            </el-option>
          </el-select>
        </div> -->
        <div class="input">
          <el-select v-model="dataForm.module" :placeholder="$t('common.please_select') + $t('share.module')" clearable>
            <el-option label="产品" value="产品"></el-option>
            <el-option label="案例" value="案例"></el-option>
            <el-option label="基地" value="基地"></el-option>
            <el-option label="讲师" value="讲师"></el-option>
            <el-option label="供应商" value="供应商"></el-option>
          </el-select>
        </div>
        <div class="input">
          <el-button icon="Search" @click="getDataList(true)">
            {{ i18n('common.search') }}
          </el-button>
        </div>
      </div>

      <div class="admin_table_main">
        <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle" stripe lazy>
          <el-table-column type="selection" header-align="center" align="center">
          </el-table-column>

          <el-table-column prop="id" :label="i18n('common.id')" width="70">
          </el-table-column>

          <el-table-column :label="i18n('organization.title')" width="140">
            <template v-slot="scope">
              <span v-if="scope.row.organization">{{ scope.row.organization.title }}</span>
            </template>
          </el-table-column>

          <el-table-column prop="group_text" :label="$t('share.category.group')" width="100">
          </el-table-column>

          <el-table-column :label="i18n('share.share_name')" min-width="200">
            <template v-slot="scope">
              <dl class="table_dl" v-if="1 == scope.row.role_id && scope.row.user">
                <dt>
                  <el-avatar :size="45" :src="scope.row.user.avatar" />
                </dt>
                <dd class="table_dl_dd_all_30">
                  {{ scope.row.user.nickname }}
                </dd>
                <dd class="table_dl_dd_all_16_gray">
                  <span v-if="scope.row.user.organization">
                    {{ scope.row.user.organization.title }}
                  </span>
                </dd>
              </dl>
              <dl class="table_dl" v-if="2 == scope.row.role_id && scope.row.contact">
                <dt>
                  <el-avatar :size="45" :src="scope.row.contact.avatar" />
                </dt>
                <dd class="table_dl_dd_all_30">
                  {{ scope.row.contact.name }}
                </dd>
                <dd class="table_dl_dd_all_16_gray">
                  <span v-if="scope.row.contact.client">
                    {{ scope.row.contact.client.name }}
                  </span>
                </dd>
              </dl>
            </template>
          </el-table-column>

          <el-table-column :label="$t('browse.name_type')" width="100">
            <template v-slot="scope">
              {{ scope.row.role_text }}
            </template>
          </el-table-column>

          <el-table-column prop="module" :label="i18n('share.module')" min-width="200">
          </el-table-column>

          <el-table-column prop="total" :label="i18n('share.total')" width="80">
          </el-table-column>

          <el-table-column :label="i18n('common.handle')" fixed="right" width="100">
            <template v-slot="scope">
              <el-button
                v-if="isAuth('module:business:share:list:list')"
                icon="Search"
                @click="router.push({ name: 'module_business_share_list_list', query: { category_id: scope.row.id } })">
                {{ i18n('share.info') }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="admin_table_main_pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :page-size="pageSize"
            :total="totalPage"
            :current-page="pageIndex"
            background
            layout="prev, pager, next,jumper,total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="share-category-list">
  import { useBase } from "@/hooks/base/useBase"
  import { useTable } from "@/hooks/table/useTable"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/share/category')

  const organizationList = ref([])
  const clientList = ref([])

  const dataForm = reactive({
    organization_id: '',
    client_id: '',
    module: '',
    create_time: '',
  })

  const {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    dataListLoading,
    sizeChangeHandle,
    currentChangeHandle,
    selectionChangeHandle,
    getDataList,
    handleStatus,
    deleteHandle,
  } = useTable(model.value, dataForm)

  const loadOrganizationList = () => {
    $http({
      url: $http.adornUrl('/organization/select'),
      method: 'get',
    }).then(({ data }) => {
      if (data && data.status === 200) {
        organizationList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadClientList = () => {
    $http({
      url: $http.adornUrl('/business/client/select'),
      method: 'get',
      params: $http.adornParams({}),
    }).then(({data}) => {
      if (data && data.status === 200) {
        clientList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  onMounted(() => {
    getDataList(model)

    loadOrganizationList()
    loadClientList()
  })
</script>
